﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TKToXML.Models
{
    public class ToKhai : BaseChungTu, IBaseChungTu
    {           
        public virtual DateTime NgayChungTu { get; set; }
        public virtual DateTime NgayDK { get; set; }
        public virtual string MaDonVi { get; set; }
        public virtual string TenDonViDoiTac { get; set; }
        //public virtual string MaHQ { get; set; }
        public virtual string MaLH { get; set; }
        public virtual int NamDK { get; set; }
        public virtual int SoTK { get; set; }
        /// <summary>
        /// Mã nội dung ủy quyền ?
        /// </summary>
        public virtual int MaNDUQ { get; set; }
        public virtual string MaCKXuat { get; set; }
        public virtual string TenCKXuat { get; set; }
        public virtual string MaCKNhap { get; set; }
        public virtual string TenCKNhap { get; set; }
        public virtual string MaPTVT { get; set; }
        public virtual string TenPTVT { get; set; }
        public virtual string MaDKGH { get; set; }
        public virtual string MaPTTT { get; set; }
        public virtual string MaNguyenTe { get; set; }
        public virtual decimal TyGiaNguyenTe { get; set; }
        public virtual string MaNuocNhapKhau { get; set; }
        public virtual string MaNuocXuatKhau { get; set; }
        public virtual string MaDonViCDGiaoHang { get; set; }
        public virtual string TenDonViCDGiaoHang { get; set; }
        public virtual string MaDonViGiaoHang { get; set; }
        public virtual string TenDonViGiaoHang { get; set; }
        public virtual string NoiGiaoHang { get; set; }
        public virtual string MaDonViNganHang { get; set; }
        public virtual string TenDonViNganHang { get; set; }
        public virtual string MaDonViNganHangTrungGian { get; set; }
        public virtual string TenDonViNganHangTrungGian { get; set; }        
        public virtual DateTime NgayNhapMay { get; set; }
        public virtual string NguoiNhapMay { get; set; }
        public virtual int TongSoKien { get; set; }
        public virtual int TongTrongLuong { get; set; }            
        public virtual string TenDonViDaiLy { get; set; }
        public virtual string MaDonViDaiLy { get; set; }
        
        
        public virtual IList<HopDongChiTiet> HopDongChiTiets { get; set; }
        
        /*public virtual string ToXML()
        { 
            string result = "";
            result += "<ContractDocuments>";
                result += "<ContractDocument>";
                    result += "<reference>" + SoHopDong + "</reference>";
                    result += "<issue>" + NgayHopDong.ToString("yyyy-MM-dd") + "</issue>";
                    result += "<expire>" + ThoiHanThanhToan.ToString("yyyy-MM-dd") + "</expire>";
                    result += "<Payment>";
                        result += "<method>" + MaPTTT + "</method>";
                    result += "</Payment>";
                    result += "<TradeTerm>";
                        result += "<condition>" + MaDKGH + "</condition>";
                    result += "</TradeTerm>";
                    result += "<DeliveryDestination>";
                        result += "<line>" + DiaDiemGH + "</line>";
                    result += "</DeliveryDestination>";
                    result += "<CurrencyExchange>";
                        result += "<currencyType>" + MaNguyenTe + "</currencyType>";
                    result += "</CurrencyExchange>";
                    result += "<totalValue>" + TriGiaNguyenTe.ToString() + "</totalValue>";
                    result += "<Buyer>";
                    result += "<name>" + TenDVMuaHang + "</name>";
                        result += "<identity>" + MaDVMuaHang + "</identity>";
                    result += "</Buyer>";
                    result += "<Seller>";
                        result += "<name>" + TenDVBanHang + "</name>";
                        result += "<identity>" + MaDVBanHang + "</identity>";
                    result += "</Seller>";
                    //Hop dong chi tiet
                    foreach (HopDongChiTiet hdct in HopDongChiTiets)
                    {
                        result += hdct.ToXML();
                    }
                result += "</ContractDocument>";
            result += "</ContractDocuments>";
            return result;
        }*/

        public virtual string ToXML()
        {
            if (MaLH.Substring(0, 1).ToUpper() == "N")
                Type = MessageType.CommercialImportDeclaration.ToString("%d");
            else
                Type = MessageType.CommercialExportDeclaration.ToString("%d");


            string result = "";
            //result = "<?xml version='1.0' encoding='UTF-8' ?><Envelope><Header><procedureType>2</procedureType>" +
            //"<Reference><version>3.00</version><messageId>" + System.Guid.NewGuid().ToString() + "</messageId></Reference>";
            //result += "<From>" +
            //            "<name>" + TenDVDL + "</name>" +
            //            "<identity>" + Ma_DV_DL.Trim() + "</identity>" +
            //          "</From>" +
            //          "<To>" +
            //            "<name>" + Ten_HQ + "</name>" +
            //            "<identity>" + MaHQ.Trim() + "</identity>" +
            //          "</To>";
            //result += "<Subject>" +
            //            "<type>" + loaiTK + "</type>" +
            //            "<function>" + (int)MessageFuntion.KhaiBao + "</function>" +
            //            "<reference>" + referenceID + "</reference>" +
            //          "</Subject></Header>";
            ////body
            //result += "<Body><Content>";
            result += "<Declaration>" +
                            "<issuer>" + Type.ToString() + "</issuer>" +
                            "<reference>" + Reference + "</reference>" +
                //Muc dich khai ngay khai bao 
                            "<issue>" + NgayDK.ToString("yyyy-MM-dd HH:mm:ss") + "</issue>" +
                            "<function>" + Function.ToString() + "</function>" +
                            "<status>" + "1" + "</status>" + //Status khong dung, luon dat la 1
                            "<declarationOffice>" + MaHQ + "</declarationOffice>";
            result += "<goodsItem>" + TongSoKien + "</goodsItem>" +
                      "<loadingList>0</loadingList>";//So luong chung tu phu luc dinh kem luon la 0
            result += "<totalGrossMass>" + TongTrongLuong + "</totalGrossMass>";
            result += "<natureOfTransaction>" + MaLH + "</natureOfTransaction>";
            result += "<paymentMethod>" + MaPTTT + "</paymentMethod>";
            result += "<Agent>" +
                            "<name>" + TenDonViDaiLy + "</name>" +
                            "<identity>" + MaDonViDaiLy.Trim() + "</identity>" +
                            "<status>" + (int)AgentStatus.DaiLyLamThuTucHQ + "</status>" + //Lay o dau
                      "</Agent>";
            result += "<Agent>" +
                        "<name>" + TenDonViDaiLy + "</name>" +
                        "<identity>" + MaDonViDaiLy.Trim() + "</identity>" +
                        "<status>" + (int)AgentStatus.NguoiKhai + "</status>" + //Lay o dau
                      "</Agent>";
            result += "<CurrencyExchange>" +
                        "<currencyType>" + MaNguyenTe + "</currencyType>" +
                        "<rate>" + TyGiaNguyenTe + "</rate>" +
                      "</CurrencyExchange>";
            result += "<DeclarationPackaging><quantity>" + TongSoKien + "</quantity></DeclarationPackaging>";
            //Khong co noi dung ve cac chung tu kem theo trong dtokhaimd
            //TODO: Đang làm dở nội dung về TK to xml nhưng bị vướng không biết lấy nội dung chứng từ kèm ở đâu
            /*
            result += "<AdditionalDocument>" +
                        "<issue>" + HopDong.Ngay_HD.ToString("yyyy-MM-dd") + "</issue>" +
                        "<reference>" + HopDong.So_HD + "</reference>" +
                        "<type>" + (int)MessageType.HopDong + "</type>" +
                        "<name>Contract</name>" +
                      "</AdditionalDocument>";
            result += "<AdditionalDocument>" +
                        "<issue>" + VanDon.Ngay_VD.ToString("yyyy-MM-dd") + "</issue>" +
                        "<reference>" + VanDon.So_VD + "</reference>" +
                        "<type>" + (int)MessageType.AirwayBill + "</type>" +
                        "<name>Air waybill</name>" +
                      "</AdditionalDocument>";
            result += "<Invoice>" +
                        "<issue>" + HoaDon.Ngay_HD.ToString("yyyy-MM-dd") + "</issue>" +
                        "<reference>" + HoaDon.So_HD + "</reference>" +
                        "<type>" + (int)MessageType.HoaDonThuongMai + "</type>" +
                      "</Invoice>";
            //Khong co noi dung ve dai ly trong bang dtokhaimd
            result += "<Importer>" +
                        "<name>" + Ten_DV + "</name>" +
                        "<identity>" + Ma_DV.Trim() + "</identity>" +
                      "</Importer>";
            result += "<RepresentativePerson>" +
                        "<contactFunction>x</contactFunction><name>x</name>" +
                      "</RepresentativePerson>";
            result += "<GoodsShipment>";
            result += "<exportationCountry>" + Ma_NU_XK + "</exportationCountry>";
            result += "<Consignor><name>" + Ten_DV_DT + "</name></Consignor>";
            result += "<Consignee>" +
                        "<name>" + TenDonViDaiLy + "</name>" +
                        "<identity>" + Ma_DV_DL.Trim() + "</identity>" +
                      "</Consignee>";
            //Noi dung trong line o dau
            result += "<DeliveryDestination><line>HCM</line></DeliveryDestination>";
            //Đặt Ten_CK hay Ten_CK_N hay Ten_CK_X
            result += "<EntryCustomsOffice><name>" + Ten_CK + "</name><code>" + Ma_CK_N + "</code></EntryCustomsOffice>";
            result += "<Exporter><name>" + Ten_DV_DL + "</name></Exporter>";
            result += "<TradeTerm><condition>" + Ma_DK_GH + "</condition></TradeTerm>";
            foreach (EDHANG Hang in EDHANGs)
            {
                string loaiHang;
                if (Hang.MaPhu.Substring(0, 1) == "N")
                    loaiHang = ((int)CommodityType.NguyenPhuLieu).ToString();
                else if (Hang.MaPhu.Substring(0, 1) == "S")
                    loaiHang = ((int)CommodityType.SanPham).ToString();
                else if (Hang.MaPhu.Substring(0, 1) == "T")
                    loaiHang = ((int)CommodityType.ThietBi).ToString();
                else if (Hang.MaPhu.Substring(0, 1) == "C")
                    loaiHang = ((int)CommodityType.TieuDung).ToString();
                else //if (Hang.Ma_Phu.Substring(0, 1) == "U")
                    loaiHang = ((int)CommodityType.LoaiKhac).ToString();
                result += "<CustomsGoodsItem>" +
                            "<customsValue>" + Hang.TriGia_KB + "</customsValue>" +
                            "<sequence>" + Hang.So_TT + "</sequence>" +
                            "<statisticalValue>" + Hang.TriGia_TT + "</statisticalValue>" +
                            "<unitPrice>" + Hang.DonGia_KB + "</unitPrice>" +
                            "<Origin>" +
                                "<originCountry>" + Hang.Ma_NU_XX.Trim() + "</originCountry>" +
                            "</Origin>" +
                            "<Commodity>" +
                                "<description>" + Hang.Ten_HH + "</description>" +
                                "<identification>" + Hang.MaPhu + "</identification>" +
                                "<tariffClassification>" + Hang.Ma_HS + "</tariffClassification>" +
                                "<type>" + loaiHang + "</type>" +
                            "</Commodity>" +
                            "<GoodsMeasure>" +
                                "<quantity>" + Hang.SoLuong + "</quantity>" +
                                "<measureUnit>" + Hang.Ma_DVT_KB.Trim() + "</measureUnit>" +
                            "</GoodsMeasure>";
                
                //Trị giá hải quan khai bắt buộc với doanh nghiệp thường                            
                result += "<CustomsValuation>";
                //Tổng chi phí khác
                result += "<exitToEntryCharge>" + TongChiPhiKhac + "</exitToEntryCharge>";
                //Phí vận tải
                result += "<freightCharge>" + PhiVanTai + "</freightCharge>";
                //Phương pháp xác định trị giá không khai báo trong trường hợp không khai báo tờ khai trị giá
                //result += "<method>" + PhuongPhapXacDinhTriGia + "</method>";
                result += "<method>" + "</method>";
                //Tổng các khoản phải cộng - tổng các khoản được trừ
                result += "<otherChargeDeduction>" + CacKhoanCongTruCacKhoanTru + "</otherChargeDeduction>";
                result += "</CustomsValuation>";
                result += "<ValuationAdjustment>";
                result += "<addition>" + MaLoaiGiaTriDieuChinh + "</addition>";
                result += "<percentage>" + PhanTramDieuChinh + "</percentage>";
                result += "<amount>" + SoTienDieuChinh + "</amount>";
                result += "</ValuationAdjustment>";
                
                result += "</CustomsGoodsItem>";
            }
            result += "</GoodsShipment></Declaration>";
            //</Content></Body></Envelope>";

            return result;*/
            return "";
        }
    }
}
